use std::collections::BTreeMap;

use http::uri;
use ruma::api::client::discovery::{
    discover_homeserver::{self, HomeserverInfo},
    get_supported_versions,
};

use crate::{server::server, Result, Ruma};

/// # `GET /.well-known/matrix/client`
///
/// Returns the client server discovery information.
pub async fn well_known_client(
    _body: Ruma<discover_homeserver::Request>,
) -> Result<discover_homeserver::Response> {
    let client_url = uri::Uri::builder()
        .scheme("https")
        .authority(server().config.server_name.as_str())
        .path_and_query("")
        .build()
        .unwrap();

    Ok(discover_homeserver::Response {
        homeserver: HomeserverInfo {
            base_url: client_url.to_string(),
        },
        identity_server: None,
    })
}

/// # `GET /_matrix/client/versions`
///
/// Get the versions of the specification and unstable features supported by this server.
///
/// - Versions take the form MAJOR.MINOR.PATCH
/// - Only the latest PATCH release will be reported for each MAJOR.MINOR value
/// - Unstable features are namespaced and may include version information in their name
///
/// Note: Unstable features are used while developing new features. Clients should avoid using
/// unstable features in their stable releases
pub async fn get_supported_versions_route(
    _body: Ruma<get_supported_versions::Request>,
) -> Result<get_supported_versions::Response> {
    let resp = get_supported_versions::Response {
        versions: vec![
            "v1.10".to_owned(),
            "v1.9".to_owned(),
            "v1.8".to_owned(),
            "v1.7".to_owned(),
            "v1.6".to_owned(),
            "v1.5".to_owned(),
            "v1.4".to_owned(),
            "v1.3".to_owned(),
            "v1.2".to_owned(),
            "v1.1".to_owned(),
            "v1.0".to_owned(),
        ],
        unstable_features: BTreeMap::from_iter([
            // ("org.matrix.e2e_cross_signing".to_owned(), true),
            // ("org.matrix.msc3916.stable".to_owned(), true),
        ]),
    };

    Ok(resp)
}
